package com.example.ch3_3.service.iml;

import com.example.ch3_3.entity.User;
import com.example.ch3_3.mapper.UserMapper;
import com.example.ch3_3.service.LoginService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Slf4j
@Service
public class LoginServiceIml implements LoginService {
    @Autowired
    private UserMapper userMapper;

    @Override
    public boolean login(String username, String password) {
        User user = userMapper.getUserByName(username);
        if (user == null || !user.getPassword().equals(password)) {
            log.info("用户名或密码错误");
            return false;
        }
        log.info("登录成功");
        return true;
    }

    @Override
    public int registerUser(User user) {
        if (userMapper.findUserByname(user.getName()) != 0) {
            throw new RuntimeException("账号已存在");
        }
        return userMapper.createUser(user);
    }
}